package com.icode.web.controller.house;

import com.icode.core.dto.house.BuildingTypeDTO;
import com.icode.core.shared.ImageUtils;
import com.icode.service.house.HouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: sunk
 * Date: 15-1-4
 * Time: 下午2:30
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping(value = "/house/houseType")
public class BuildingTypeController {
    @Autowired
    private HouseService houseService;

    @RequestMapping(value = "form/{baseGuid}", method = RequestMethod.GET)
    private ModelAndView showForm(@PathVariable String baseGuid) {
        BuildingTypeDTO buildingTypeDTO = new BuildingTypeDTO();
        buildingTypeDTO.setHouseBaseGuid(baseGuid);
        return new ModelAndView("back/houseType/houseTypeForm", "houseTypeDTO", buildingTypeDTO);
    }

    @RequestMapping(value = "form", method = RequestMethod.POST)
    private String showForm(@ModelAttribute(value = "buildingTypeDTO") @Valid BuildingTypeDTO buildingTypeDTO, BindingResult bindingResult) throws IOException {
        String redirect = "";
        if (bindingResult.hasErrors()) {
            redirect = "back/houseType/houseTypeForm";
        } else {
            houseService.saveOrUpdateBuildingType(buildingTypeDTO);
            redirect = "redirect:/house/houseType/list/" + buildingTypeDTO.getHouseBaseGuid();
        }
        return redirect;
    }

    @RequestMapping(value = "before_update/{buildingTypeGuid}/{houseBaseGuid}", method = RequestMethod.GET)
    private ModelAndView beforeUpdate(@PathVariable String buildingTypeGuid, @PathVariable String houseBaseGuid, Model model) {
        BuildingTypeDTO buildingTypeDTO = houseService.findBuildingTypeDTOByGuid(buildingTypeGuid);
        model.addAttribute("houseBaseGuid", houseBaseGuid);
        return new ModelAndView("back/houseType/houseTypeForm", "houseTypeDTO", buildingTypeDTO);
    }

    @RequestMapping(value = "list/{baseGuid}", method = RequestMethod.GET)
    private String showBuildingTypes(@PathVariable String baseGuid, Model model) {
        List<BuildingTypeDTO> buildingTypeDTOs = houseService.getBuildingTypeDTOsByBaseGuid(baseGuid);
        model.addAttribute("list", buildingTypeDTOs);
        model.addAttribute("houseBaseGuid", baseGuid);
        return "back/houseType/buildingTypeOverview";
    }

    @RequestMapping(value = "add/{baseGuid}", method = RequestMethod.GET)
    private ModelAndView add(@PathVariable String baseGuid) {
        return new ModelAndView("back/houseType/houseTypeForm", "houseTypeDTO", new BuildingTypeDTO(baseGuid));
    }

    @RequestMapping(value = "get/{guid}", method = RequestMethod.GET)
    public void showImage(@PathVariable String guid, HttpServletResponse response) {
        byte[] contents = houseService.getImageByGuid(guid);
        ImageUtils.showImageByContents(contents, response);
    }

    @RequestMapping(value = "delImg/{imageGuid}", method = RequestMethod.GET)
    @ResponseBody
    private String delImg(@PathVariable String imageGuid) {
        houseService.deleteBuildingTypeImageByGuid(imageGuid);
        return "success";
    }

    @RequestMapping(value = "delBuildingType", method = RequestMethod.POST)
    @ResponseBody
    private String delBuildingType(String buildingTypeGuid) {
        houseService.deleteBuildingTypeByGuid(buildingTypeGuid);
        return "success";
    }
}
